/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leulounge.views;

/**
 *
 * @author Salaver
 */
import com.leulounge.control.Login;
import com.leulounge.model.Client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;

/** @see http://stackoverflow.com/a/3002830/230513 */
public class OptionalBilling {

    public void display() {
//        # Compañía (string) o ""|          Tienda/Caja (String) o ""| Vendedor (String) o ""| Cod_Cliente (String) o ""| Nombre_Cliente (String)| Direccion (String) o ""|          RIF_Cliente (string)| Fecha_Factura (string dd/mm/yyyy) o ""| Num_Factura (String) o ""| Es_Devolucion (1/0)| IdentificadorUnico (String) o ""| Ubicacion (String) o ""| Es_NoFiscal (1/0) | Fecha Factura a Devolver (DD/MM/YYYY) o "" | Hora Factura a Devolver (HH:MM) o "" | Número de Comprobante a Devolver (String) o ""  | Serial Impresora en el que se imprimió la factura devuelta (String) o ""
//        "Totalsoftware de Venezuela, C.A."| "Caracas"|                "Pedro Perez"|          "00001"|                  "Cangrejito, C.A."|       "Las Palmas, Caracas, Venezuela"| "J000000000"|         "16/12/2006"|                           "0102030405"|              0|                   "0001"|                           "M1"|                    0 |                 "" |                                         "" |                                   "" |                                              ""
        JTextField field1 = new JTextField("");
        JTextField field2 = new JTextField("");
        JTextField field3 = new JTextField("");
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("¿Desea una factura personalizada?"));
        panel.add(new JLabel("Cliente:"));
        panel.add(field1);
        panel.add(new JLabel("Dirección:"));
        panel.add(field2);
        panel.add(new JLabel("Rif:"));
        panel.add(field3);
        Object[] options = {"Si","No","Cancelar"};
        int result = JOptionPane.showOptionDialog(Login.menu,
        panel,
        "Impresion Personalizada",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
        options[2]);
        if (result == JOptionPane.YES_OPTION) {
            if ( (field1.getText().equals("")) ||  (field2.getText().equals("")) ||  (field3.getText().equals(""))){
                JOptionPane.showMessageDialog(Login.menu,
                "Error preparando factura personalizada, no se llenaron los campos correctamente",
                "Error de impresion personalizada",
                JOptionPane.ERROR_MESSAGE);
            } else {
                Object[] options2 = {"Si, imprimir",
                    "No, cancelar"};
                int n = JOptionPane.showOptionDialog(Login.menu,
                "Esta por imprimir una factura personalizada, ¿Esta seguro?",
                "Impresion personalizada",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,     
                options2, 
                options2[0]);
                if (n == JOptionPane.YES_OPTION){
                    final Client client = new Client();
                    client.setCliente(field1.getText());
                    client.setDireccion(field2.getText());
                    client.setRif(field3.getText());
                    final JDialog loading = new JDialog(Login.menu);
                    JPanel p1 = new JPanel(new BorderLayout());
                    JLabel label = new JLabel("...POR FAVOR ESPERE, IMPRIMIENDO FACTURA...");
                    label.setForeground(Color.red);
                    p1.add(label, BorderLayout.CENTER);
                    loading.setUndecorated(true);
                    loading.getContentPane().add(p1);
                    loading.pack();
                    loading.setLocationRelativeTo(Login.menu);
                    loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                    loading.setModal(true);

                    SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
                        @Override
                        protected String doInBackground() throws InterruptedException{
                            Login.printer.run("bill", client);
                            return null;
                        }
                        @Override
                        protected void done() {
                            loading.dispose();
                        }
                    };
                    worker.execute();
                    loading.setVisible(true);
                    try {
                        worker.get();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }  
        } else if (result == JOptionPane.NO_OPTION) {
            Object[] options2 = {"Si, imprimir",
                    "No, cancelar"};
                int n = JOptionPane.showOptionDialog(Login.menu,
                "Esta por imprimir una factura no personalizada, ¿Esta seguro?",
                "Impresion no personalizada",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,     
                options2, 
                options2[0]);
                if (n == JOptionPane.YES_OPTION){
                    final Client client = new Client();
                    client.setCliente("");
                    client.setDireccion("");
                    client.setRif("");
                    final JDialog loading = new JDialog(Login.menu);
                    JPanel p1 = new JPanel(new BorderLayout());
                    JLabel label = new JLabel("...POR FAVOR ESPERE, IMPRIMIENDO...");
                    label.setForeground(Color.red);
                    p1.add(label, BorderLayout.CENTER);
                    loading.setUndecorated(true);
                    loading.getContentPane().add(p1);
                    loading.pack();
                    loading.setLocationRelativeTo(Login.menu);
                    loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                    loading.setModal(true);

                    SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
                        @Override
                        protected String doInBackground() throws InterruptedException{
                            Login.printer.run("bill", client);
                            return null;
                        }
                        @Override
                        protected void done() {
                            loading.dispose();
                        }
                    };
                    worker.execute();
                    loading.setVisible(true);
                    try {
                        worker.get();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
        }
    }
}
